#include "mainwindow.h"
#include "pub_var.h"
#include "dialog_signin.h"
#include <QMessageBox>
#include <QApplication>
#include <QDebug>
#include <QDateTime>
#include <QMutex>
#include <QTextCodec>
#include <QDir>

void outputMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
  static QMutex mutex;
  mutex.lock();

  QString text;
  switch(type)
  {
  case QtDebugMsg:
      text = QString("Debug:");
      break;

  case QtWarningMsg:
      text = QString("Warning:");
      break;

  case QtCriticalMsg:
      text = QString("Critical:");
      break;

  case QtFatalMsg:
      text = QString("Fatal:");
  }

  QString context_info = QString("File:(%1) Line:(%2)").arg(QString(context.file)).arg(context.line);
  //QString current_date_time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
  QString current_date_time = QDateTime::currentDateTime().toString("hh:mm:ss");
  QString current_date = QString("(%1)").arg(current_date_time);
  QString message = QString("%1 %2 %3 %4").arg(current_date).arg(text).arg(context_info).arg(msg);

 QString fileName = "logs/" + QDate::currentDate().toString("yyyyMMdd") + ".log";

  QFile file(fileName);
  file.open(QIODevice::WriteOnly | QIODevice::Append);
  QTextStream text_stream(&file);
  text_stream << message << "\r\n";
  file.flush();
  file.close();

  mutex.unlock();
}


int main(int argc, char *argv[])
{

    //注册MessageHandler
    qInstallMessageHandler(outputMessage);

    QApplication a(argc, argv);
     // utf8/gbk/gb18030;
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("gb18030")) ;

    //查看基础路径是否存在，如不存在则建立
    //日志文件
    QString basePath = QCoreApplication::applicationDirPath() + "/logs";
    QDir d = QDir(basePath);
    if(!d.exists()){
        d.mkdir(basePath);
    }

    //下载路径
    basePath = QCoreApplication::applicationDirPath() + "/download";
    d = QDir(basePath);
    if(!d.exists()){
        d.mkdir(basePath);
    }

    //下载文件归档路径
    basePath.append("/back");
    d = QDir(basePath);
    if(!d.exists()){
        d.mkdir(basePath);
    }

    //数据库连接
    QSettings setIni("clin.ini", QSettings::IniFormat);
    QString host,port,dbnm, usr, pswd;

    //MYSQL数据库
    host = setIni.value("database/m_hostname").toString();
    port = setIni.value("database/m_port").toString();
    dbnm = setIni.value("database/m_databasename").toString();
    usr = setIni.value("database/m_username").toString();
    pswd = setIni.value("database/m_password").toString();

    pub::db_m.setPort(port.toInt());
    pub::db_m.setHostName(host);
    pub::db_m.setPassword(pswd);
    pub::db_m.setUserName(usr);
    pub::db_m.setDatabaseName(dbnm);
    if(!pub::db_m.open()){
        QString tmp = "Can not open database ！ \nPort: "+port;
        tmp = tmp + "\nHost: "+host;
        tmp = tmp + "\nDBName: "+dbnm;
        QMessageBox::information(NULL,"注意",tmp);
    }

    //ORACLE数据库
    host = setIni.value("database/o_hostname").toString();
    port = setIni.value("database/o_port").toString();
    dbnm = setIni.value("database/o_databasename").toString();
    usr = setIni.value("database/o_username").toString();
    pswd = setIni.value("database/o_password").toString();
    pub::db_o.setPort(port.toInt());
    pub::db_o.setHostName(host);
    pub::db_o.setPassword(pswd);
    pub::db_o.setUserName(usr);
    pub::db_o.setDatabaseName(dbnm);
    if(!pub::db_o.open()){
        QString tmp = "Can not open database ！ \nPort: "+port;
        tmp = tmp + "\nHost: "+host;
        tmp = tmp + "\nDBName: "+dbnm;

        QMessageBox::information(NULL,"注意",tmp);
    }


    //用户登录
    Dialog_signin sign;
    sign.exec();
    if(!pub::userValidated){
        //用户验证失败，直接退出
        return 0;
    }

    //用户验证通过，打开主窗口
    MainWindow w;
    w.show();
    return a.exec();
}
